package com.longxiao.manager.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.longxiao.exception.LXException;
import com.longxiao.service.StudentLectureService;
import com.longxiao.service.UserService;
import com.longxiao.vo.DwzResult;
import com.longxiao.vo.PageVo;
import com.longxiao.vo.StuUserVo;

@Controller
@RequestMapping(value = "student")
public class StudentController extends BaseController {
	
	@Resource
	private StudentLectureService studentLectureService;
	
	@Resource
	private UserService userService;
	
	@RequestMapping(value = "list")
	public ModelAndView toListPage(HttpServletRequest request,HttpServletResponse response) {
		return new ModelAndView("student/list");
	}
	
	@RequestMapping(value = "toRechargeLecture")
	public ModelAndView toRechargeLecture(String stuId,HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("student/rechargeLecture");
		try {
			mav.addObject("stuUserVo", userService.getStuInfo(stuId));
		} catch (LXException e) {
			logger.error("获取用户信息失败：" + e.getMessage(),e);
		}
		return mav;
	}
	
	@RequestMapping(value = "getList",method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView getList(
			@RequestParam(value = "mobile",required = false)String mobile,
			@RequestParam(value = "email",required = false)String email,
			@RequestParam(value = "nickname",required = false)String nickname,
			@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",required = false,defaultValue = "20")int pageSize,
			HttpServletRequest request) {
		StuUserVo stuUserVo = new StuUserVo();
		stuUserVo.setEmail(email);
		stuUserVo.setMobile(mobile);
		stuUserVo.setNickname(nickname);
		logger.info("获取学生信息：{}",stuUserVo);
		int count = studentLectureService.countStuUserVoList(stuUserVo);
		PageVo<StuUserVo> pageInfo = new PageVo<StuUserVo>(pageNum, count);
		List<StuUserVo> userVoList = studentLectureService.getStuUserVoList(stuUserVo, pageInfo.getOffset(), pageInfo.getRows());
		
		ModelAndView mav = new ModelAndView("student/list");
		mav.addObject("totalCount", count);
		mav.addObject("stuList", userVoList);
		mav.addObject("currentPage", pageNum);
		mav.addObject("stuUserVo", stuUserVo);
		
		initParam(request,mav);
		return mav;
	}
	
	@RequestMapping(value = "rechargeLecture")
	@ResponseBody
	public DwzResult<Void> rechargeLecture(String stuId,Integer rechargeLecture,HttpServletRequest request) {
		logger.info("为用户充值课时：{}，{}",stuId,rechargeLecture);
		try {
			if(rechargeLecture == null || rechargeLecture <= 0) {
				throw new Exception("充值课时数据错误");
			}
			boolean res = studentLectureService.rechargeLecture(stuId, rechargeLecture);
			if(res) {
				return DwzResult.success();
			}
		} catch(Exception e) {
			logger.error("系统异常：" + e.getMessage(),e);
			return DwzResult.fail(e.getMessage());
		}
		return DwzResult.fail("系统异常");
	}
}
